import Vue from 'vue'
import App from './App.vue'
import router from './router'
import Router from 'vue-router'

import i18n from './i18n'
import locale from 'element-ui/lib/locale/lang/en'
import lang from 'umy-ui/lib/locale/lang/en'

import ElementUI from 'element-ui'
import UmyUi from 'umy-ui'
import ViewUI from 'view-design';

import Viewer from 'v-viewer'
import Vuex from 'vuex'
import store from './store/store'
import socket from '../src/utils/signalSocket/signal';
import echarts from 'echarts'

import '@babel/polyfill';
import './assets/font/iconfont.css'
import '../public/scene/common.css'
import 'viewerjs/dist/viewer.css'
import 'view-design/dist/styles/iview.css';
import 'umy-ui/lib/theme-chalk/index.css';
import 'element-ui/lib/theme-chalk/index.css'
import '../src/element_ui/element-variables.scss'
import { downFileByBlob } from '@/utils/downFIleByBlob'
import { getLocalStorage } from "@/utils/storage";


const routerPush = Router.prototype.push
Router.prototype.push = function push(location) {
	return routerPush.call(this, location).catch(error => error)
}

Vue.prototype.$echarts = echarts
Vue.prototype.$socket = socket;
Vue.config.productionTip = false

Vue.use(ViewUI);
Vue.use(ElementUI, { locale })
Vue.use(UmyUi, { lang })
Vue.use(socket);
Vue.use(Vuex)
Vue.use(Viewer);

function aa(text, end) {
	text = text.replace(/" '/g, '');

	let s = /^(\.*)(\d+)(\.?)(\d{0,9}).*$/g;
	switch (end) {
		case 1:
			s = /^(\.*)(\d+)(\.?)(\d{0,1}).*$/g;
			break;
		case 2:
			s = /^(\.*)(\d+)(\.?)(\d{0,2}).*$/g;
			break;
		case 3:
			s = /^(\.*)(\d+)(\.?)(\d{0,3}).*$/g;
			break;
		case 4:
			s = /^(\.*)(\d+)(\.?)(\d{0,4}).*$/g;
			break;
		case 5:
			s = /^(\.*)(\d+)(\.?)(\d{0,5}).*$/g;
			break;
		case 6:
			s = /^(\.*)(\d+)(\.?)(\d{0,6}).*$/g;
			break;
		case 7:
			s = /^(\.*)(\d+)(\.?)(\d{0,7}).*$/g;
			break;
		case 8:
			s = /^(\.*)(\d+)(\.?)(\d{0,8}).*$/g;
			break;
		case 9:
			s = /^(\.*)(\d+)(\.?)(\d{0,9}).*$/g;
			break;
		default:
			break;
	}
	var reg = new RegExp(s)
	if (reg.test(text)) { //正则匹配通过，提取有效文本
		text = text.replace(reg, '$2$3$4').replace(/^0+/, '0');
	} else { //正则匹配不通过，直接清空
		text = '';
	}
	return text; //返回符合要求的文本（为数字且最多有带2位小数）
}

// 自定义指令，限制规则-长度-数字-英文v-myNumInput='6-2'
Vue.directive('myNumInput', {
	inserted(el, num) {
		el.firstElementChild.addEventListener('input', (e) => {
			// console.log(e);
			// console.log(e.target);
			// console.log(e.target.value);
			// setTimeout(() => {
			// 	e.target.value = ''
			// 	const inputEvent = new Event('input');
			// 	e.target.dispatchEvent(inputEvent);
			// }, 3000);

			try {
				if (e.data.length > 1) {
					e.target.value = '　';
					// 触发 input 事件，通知 Vue 数据已经改变  
					const inputEvent = new Event('input');
					e.target.dispatchEvent(inputEvent);
					return
				}
			} catch (error) {

			}
			let arr = num.expression.split('-');
			if (e.target.value.indexOf("'") != -1) {
				return
			}
			e.target.value = aa(e.target.value);
			if (arr[1] == 0 && e.target.value.toString().indexOf('.') != -1) {
				e.target.value = e.target.value.toString().substr(0, e.target.value.toString().length - 1)
			}
			if (e.target.value.toString().indexOf('.') == -1) {  //没有小数点
				if (e.target.value.toString().length > arr[0]) {
					e.target.value = e.target.value.toString().substr(0, arr[0]);
				}
			} else {
				try {
					let testArr = e.target.value.toString().split('.');
					e.target.value = `${testArr[0].substr(0, arr[0])}.${testArr[1].substr(0, arr[1])}`
				} catch (error) {
				}
			}
			const inputEvent = new Event('input');
			e.target.dispatchEvent(inputEvent);
		})
	}
})
// 自定义指令，限制规则-长度-数字-英文v-myNumInput='6-2'
Vue.prototype.$setMyNum = (inner, num) => {
	console.log(inner);
	console.log(num);
	let type = '';
	if (typeof inner === 'number') {
		type = 'number';
	}
	if (typeof inner === 'string') {
		type = 'string';
	}
	let arr = num.split('-');
	if (inner.indexOf("'") != -1) {
		return
	}

	inner = aa(inner, arr[1]);
	console.log(inner);
	// 只有整数位，并且有小数点
	if (arr[1] == 0 && inner.toString().indexOf('.') != -1) {
		inner = inner.toString().split('.')[0].substr(0, arr[0])
		// return Number(inner)
	} else if (inner.toString().indexOf('.') == -1) {  //没有小数点
		if (inner.toString().length > arr[0]) {
			inner = inner.toString().substr(0, arr[0]);
			// return Number(inner)
		}
		// 有小数点，并且小数点位数大于1
	} else {
		try {
			let testArr = inner.toString().split('.');
			inner = `${testArr[0].substr(0, arr[0])}.${testArr[1].substr(0, arr[1])}`
		} catch (error) {
		}
	}

	if (type === 'number') {
		return parseInt(inner)

	} else {
		return inner.toString()
	}
};


Vue.prototype.$setMaxLength = (inner, maxLength) => {
	// 根据字节断定长度
	// inner = inner.toString().replace(/[\/\:\：\”\“\*\?\"\<\>\\\|\:\？\"\<\>\?]/g, '');
	let len = inner.toString().replace(/[^\x00-\xff]/g, 'xx').length;
	console.log(len);

	let nowStr = '';
	let nowLen = 0;
	for (let i = 0; i < inner.toString().length + 1; i++) {
		nowStr = inner.toString().substr(0, i);
		nowLen = nowStr.replace(/[^\x00-\xff]/g, 'xx').length;

		if (nowLen == maxLength) {
			break;
		}
		if (nowLen == maxLength + 1) {
			nowStr = nowStr.substr(0, nowStr.length - 1)
			break;
		}
	}
	console.log('This is my global function!');
	return nowStr

};

Vue.prototype.$downFileByBlob = (params) => {
	downFileByBlob(params)
};

// 指定されたSetIDのオプションに対応しているか判定する
Vue.prototype.$hasOption = (option) => {
	let userData = getLocalStorage("userData");
	return userData.setIds.includes(option);
};

// 引数で指定されたパスの権限を取得する
Vue.prototype.$specifiedMenuPermission = (tMenuPath) => {
	let userData = getLocalStorage("userData");
	var specifiedMenu = userData.tMenuWeb.find(d => d.path == tMenuPath);
	var permiss = typeof specifiedMenu !== undefined ? userData[specifiedMenu.systemName.toLowerCase()] : 1;
	return permiss;
};

// 指定されたパスの権限が有効か判定する
Vue.prototype.$isEnablePermission = (tMenuPath) => {
	return Vue.prototype.$specifiedMenuPermission(tMenuPath) == 1;
}

// 指定されたパスの権限に閲覧権限があるか判定する
Vue.prototype.$hasEditPermission = (tMenuPath) => {
	return Vue.prototype.$specifiedMenuPermission(tMenuPath) == 2;
}

new Vue({
	router,
	i18n,
	store,
	render: h => h(App)
}).$mount('#app')